#/bin/bash
# ubuntu or debain 
# 将默认shell从dash更改为bash。（bash支持C语言格式的for循环）
# sudo dpkg-reconfigure dash

path=$3
FileNum="$2"
echo $(date "+%Y-%m-%d %H:%M:%S") "3 ===== ${path} 2 ===== ${FileNum}" >>${HOME}/.aria2/log.txt

Localdir="/data/downloads/bts /data/downloads/movies /data/downloads/os /data/downloads/soft /data/downloads/mp3"

mv_dira() {
    # 多文件情况
    echo $(date "+%Y-%m-%d %H:%M:%S") "path ===== ${path}" >>${HOME}/.aria2/log.txt
    ypath=$path
    path=${path%/*}
    for i in ${Localdir[@]}; do
        if [ "$path" = "$i" ]; then
            mkdir -p "${path}-ok"
            chmod -R 777 "${path}-ok"
            echo $(date "+%Y-%m-%d %H:%M:%S") "mv dir ${ypath} to ${path}" >>${HOME}/.aria2/log.txt
            mv -f "${ypath}" "${path}-ok/"
            exit 0
        fi
        #echo $(date "+%Y-%m-%d %H:%M:%S") "path=${path};" >>${HOME}/.aria2/log.txt
        #echo $(date "+%Y-%m-%d %H:%M:%S") "filepath=${filepath};" >>${HOME}/.aria2/log.txt
    done
}

mv_dir() {
    echo $(date "+%Y-%m-%d %H:%M:%S") "run mv_dir_file" >>${HOME}/.aria2/log.txt
    if [ "$FileNum" -eq 0 ]; then
        echo $(date "+%Y-%m-%d %H:%M:%S") "FileNum=0 exit 0" >>${HOME}/.aria2/log.txt
        exit 0
    fi

    filepath=$path

    # 单文件情况
    path=${path%/*}
    for i in ${Localdir[@]}; do
        if [ "$path" = "$i" ] && [ "$FileNum" -eq 1 ]; then
            mkdir -p "${path}-ok"
            chmod -R 777 "${path}-ok"
            echo $(date "+%Y-%m-%d %H:%M:%S") "mv file ${filepath} to ${path}" >>${HOME}/.aria2/log.txt
            mv -f "${filepath}" "${path}-ok/"
            exit 0
        fi
    done

    mv_dira    
    mv_dira    
}

echo $(date "+%Y-%m-%d %H:%M:%S") "3 ===== ${path} 2 ===== ${FileNum}" >>${HOME}/.aria2/log.txt
mv_dir
